package cn.ilikes.tools.nosql.jpa.springs;

import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.persistence.Query;

import cn.ilikes.tools.nosql.jpa.persistences.IJPASQLEntityManager;
import cn.ilikes.tools.nosql.jpa.query.pages.PageEntitys;

public abstract class BaseQuery extends BEntrtyManager implements IBaseQuery {

	@Override
	public <T> List<T> findByHSQL(String HSQL, int... rowsAndSize) throws SpringErrorException {
		Query query = hsqlQuery(HSQL);
		return excQuery(query, null, rowsAndSize);
	}

	@Override
	public <T> List<T> findByHSQL(String HSQL, Map<String, Object> values, int... rowsAndSize) throws SpringErrorException {
		Query query = hsqlQuery(HSQL);
		return excQuery(query, values, rowsAndSize);
	}

	@Override
	public PageEntitys findPageByHSQL(String HSQL, Map<String, Object> values, Properties properties, int size) throws SpringErrorException {
		IJPASQLEntityManager jpasqlEntityManager = getEntityManager();
		Query query = jpasqlEntityManager.createPagesQuery(HSQL, properties);
		return excPageQuery(query, values, size);
	}

	@Override
	public <T> List<T> findByQName(String QName, int... rowsAndSize) throws SpringErrorException {
		Query query = createNamedQuery(QName);
		return excQuery(query, null, rowsAndSize);
	}

	@Override
	public <T> List<T> findByQName(String QName, Map<String, Object> values, int... rowsAndSize) throws SpringErrorException {
		Query query = createNamedQuery(QName);
		return excQuery(query, values, rowsAndSize);
	}

	@Override
	public <T> T findSingleByHSQL(String HSQL, Map<String, Object> values) throws SpringErrorException {
		Query query = hsqlQuery(HSQL);
		return (T) exceteSingleQuery(query, values);
	}

	@Override
	public <T> T findSingleByQName(String QName, Map<String, Object> values) throws SpringErrorException {
		Query query = createNamedQuery(QName);
		return (T) exceteSingleQuery(query, values);
	}

}
